<template>
  <div>
    <h3 style="text-align: center">查询待供应订单</h3>
    <!--  查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <form style="text-align: right">
        <el-button type="primary" @click="searchSupplier">查询</el-button>
      </form>
    </el-form>
    <el-table
        :data="supplierAry"
        style="width: 100%">
      <el-table-column
          prop="supplierId"
          label="供应商id"
          width="100">
      </el-table-column>
      <el-table-column
          prop="companyName"
          label="供应商名称"
          width="150">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="联系电话"
          width="150">
      </el-table-column>
      <el-table-column
          prop="address"
          label="公司地址"
          width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="success"
              @click="purchaseById(scope.row.supplierId)"
          >查看待供应订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    待供应订单面板-->
    <el-dialog
        title="待供应订单信息"
        :visible.sync="dialogVisible"
        width="50%">

      <el-table
          :data="purchaseAry"
          style="width: 100%">
        <el-table-column
            prop="purchaseOrderNumber"
            label="采购订单编号"
            width="150">
        </el-table-column>
        <el-table-column
            prop="totalAmount"
            label="总数量"
            width="100">
        </el-table-column>
        <el-table-column
            prop="totalPrice"
            label="总金额"
            width="100">
        </el-table-column>
        <el-table-column
            prop="payMethod"
            label="支付方式"
            width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.payMethod=='P002-1'">余额支付</span>
            <span v-else-if="scope.row.payMethod=='P002-2'">微信支付</span>
            <span v-else>现金支付</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="success"
                @click="updateById(scope.row.purchaseId,scope.row.supplierId)"
            >发货
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

<!--    分页插件-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {

  data(){
    return{
      //当前页号
      currentPage: 1,
      //总记录数
      total: 100,
      //当前页大小
      pageSize: 5,
      //页大小列表
      pageSizes: [2, 5, 10, 20],
      //查询供应商
      supplierAry: [],
      //待供应订单页面默认隐藏
      dialogVisible: false,
      //待供应订单数组
      purchaseAry: []

    }
  },
  methods:{
    handleSizeChange(val) {
      this.pageSize = val
      this.searchSupplier()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.searchSupplier()
    },

    //分页查询所有供应商
    async searchSupplier(){
      const {data:res}=
          await this.$http.post(`/community/supplier/list?pageNo=`+this.currentPage+`&pageSize=`+this.pageSize)
      // console.log(res)
      this.supplierAry = res.rows
      this.total = res.total
    },
    //根据供应商id查询出所有待发货的采购单
    async purchaseById(id){
      this.dialogVisible = true
      const {data:res}=
          await this.$http.post(`/community/purchaseOrder/purchaseById?id=`+id)
      // console.log(res)
      this.purchaseAry = res
    },
    //根据采购id发货
    updateById(id,supplierId){

      this.$confirm('确定要发货吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const {data:res}=
            await this.$http.post(`/community/purchaseOrder/updateById?id=`+id)
        this.$message({
          type: 'success',
          message: '发货成功!'
        });
        //更新表格数据
        this.purchaseById(supplierId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消发货'
        });
      });
    }
  }

}
</script>

<style scoped>

</style>